package com.nova.scenic.projectlibs.util;

import android.util.SparseArray;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by scenic on 2016/8/27.
 */
public class StringWrapper {
    final StringBuffer stringbuffer;
//    final SparseArray<String> mResult = new SparseArray();
    final Map<Integer,String> mResult = new HashMap<>();
    private String result;
    private String fileSeparator = System.getProperty("file.separator");
    public StringWrapper(String string){
        if(string != null){
            this.stringbuffer = new StringBuffer(string);
        }else {
            stringbuffer = null;
        }
    }



    public StringWrapper fileSeparator(String separator){
        fileSeparator = separator;
        return this;
    }
    public StringWrapper fileName(){
        result = stringbuffer.substring(stringbuffer.lastIndexOf(fileSeparator) + 1);
        return this;
    }

    public StringWrapper fileSuffix(){
        result = stringbuffer.substring(stringbuffer.lastIndexOf(".") + 1);
        return this;
    }

    public <T extends StringWrapper> StringWrapper compose(Transformer<T> transformer){
        transformer.call((T) this);
        return this;
    }



    public StringWrapper save(int id){
        mResult.put(id,result);
        return this;
    }

    public String peek(int id) {
        result = mResult.get(id);
        return result;
    }

    public String peek() {
        return result;
    }


    @Override
    public String toString() {
        return result;
    }

    public interface Transformer<T>  {
        // cover for generics insanity
        void call(T t);
    }

}
